package com.example.demo.gangOfFour_design.strategy_celue.context;

import com.example.demo.gangOfFour_design.strategy_celue.calc.Calc;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author dbin
 * @Date 2020/1/9
 **/
@Slf4j
@Service
public class SuperContext {
    private Map<String, Calc> objMap;

    @Autowired
    SuperContext(ObjectProvider<List<Calc>> calcProvider)  {
        objMap = Maps.newHashMap();//new HashMap<>();
        List<Calc> calcs =calcProvider.getIfAvailable();

        if(calcs == null || calcs.size() < 1){
            System.out.println("is null");
        }

        for (Calc calc : calcs) {
            objMap.put(calc.getName(), calc);
        }

    }

    public int doAction(String type, int a, int b){
        return objMap.get(type).doOption(a, b);
    }


}
